import { GameMgr } from "./GameMgr";
import { Point, Terrain } from "./core/Point";


// 定义地形比例
const terrainRatios: { [key in Terrain]: number } = {
    [Terrain.KONG]: 0.55,
    [Terrain.SHAN]: 0.15,
    [Terrain.SHUI]: 0.13,
    [Terrain.LIN]: 0.1,
    [Terrain.SHA]: 0.05,
    [Terrain.JING]: 0.02 // 水晶单独处理
};


class MapCreator{

    // 地图半径
    radius = 15

    // 生成地图数据
    generateMap(gameMgr:GameMgr):Array<Point> {
        const list = new Array<Point>();
        for (let y = -this.radius; y <= this.radius; y++) {
            let is_ban = false; // 是否+0.5
            if (y%2 == 0) {
                is_ban = true;
            }
            for (let x = -this.radius; x <= this.radius; x++) {
                let xx = x + (is_ban ? 0 : 0.5)
                if (Math.pow(xx,2) + Math.pow(y,2) > Math.pow(this.radius,2)) {
                    continue;
                }
                let point = new Point(gameMgr, xx, y, this.randomTerrain())
                list.push(point)
            }
        }
        return list;
    }

    /** 随机地形 */
    randomTerrain() {
        const randNum = Math.random();
        let cumulativeRatio = 0;
        let terrain: Terrain = Terrain.KONG;
        for (const [t, ratio] of Object.entries(terrainRatios)) {
            cumulativeRatio += ratio;
            if (randNum < cumulativeRatio) {
                terrain = parseInt(t) as Terrain;
                break;
            }
        }
        return terrain.valueOf();
    }


    /***************************** 直接读取本地的 **************************************/
    arr_points = require("../../地图文件/map.json")
    getArrPoints(gameMgr:GameMgr) {
        let arr_points = new Array()
        for(var p of this.arr_points) {
            arr_points.push(new Point(gameMgr,p.x,p.y,p.terrain))
        }
        return arr_points
    }
}

export default new MapCreator();
